<script setup>
import { ref, watch, onMounted } from 'vue'
import { getPurchasePageAPI } from '@/apis/purchase'

let categoryList = [
    "全部", "水果", "蔬菜", "种子种苗", "化肥", "水产",
    "粮油米面", "农副加工", "禽畜肉蛋"
]

const purchaseList = ref([])
const page = ref({
    current: 1,
    pageSize: 10,
    total: 100
})

watch(
    () => page.value.current,
    (newVal) => {
        getPage(newVal)
    }
)

const getPage = async (pageNum) => {
    const res = await getPurchasePageAPI({ page: pageNum })
    // console.log(res)
    if (res.code === 2000) {
        purchaseList.value = res.data.records
        page.value.total = res.data.total
    }
}

onMounted(() => {
    getPage(1)
})
</script>

<template>
<div class="purchase-page">
    <div class="page-header">
        <div class="sort page-header-box">
            <div class="box-title">分类</div>
            <div class="box-content">
                <a href="#" v-for="(category, i) in categoryList" :key="i" :class="{active: i === 0}">{{ category }}</a>
            </div>
        </div>
        <div class="purchase-list">
            <div class="list-header">
                <div class="col-1">采购名称</div>
                <div class="col-2">采购数量</div>
                <div class="col-3">采购说明</div>
                <div class="col-4">期望来源地</div>
                <div class="col-5">采购方</div>
                <div class="col-6">发布时间</div>
                <div class="col-7">操作</div>
            </div>
            <template v-for="purchase in purchaseList" :key="purchase.id">
                <RouterLink :to="`/purchaseDetail/${purchase.id}`" class="list-item">
                    <div class="col col-1">{{ purchase.name }}</div>
                    <div class="col col-2">{{ purchase.amount }}{{ purchase.unit }}</div>
                    <div class="col col-3">{{ purchase.comment }}</div>
                    <div class="col col-4">{{ purchase.origin }}</div>
                    <div class="col col-5">{{ purchase.user }}</div>
                    <div class="col col-6">{{ purchase.publishTime.split(' ')[0] }}</div>
                    <div class="col col-7">
                        <a href="#">查看详情</a>
                    </div>
                </RouterLink>
            </template>
        </div>
        <div class="pagination">
            <el-pagination 
                background 
                layout="prev, pager, next" 
                v-model:page-size="page.pageSize"
                v-model:current-page="page.current"
                :total="page.total" 
            />
        </div>
    </div>
</div>
</template>

<style scoped lang='scss'>
.purchase-page {
    width: 1280px;
    margin: 20px auto;
    padding: 20px;
    background-color: #fff;

    .page-header {
        width: 100%;

        .page-header-box {
            display: flex;
            width: 100%;
            min-height: 40px;
            margin-bottom: 20px;
            border-bottom: 1px dashed #d8d8d8;

            .box-title {
                height: 100%;
                width: 90px;
                padding-top: 3px;
            }

            .box-content {
                width: 1150px;
                display: flex;
                flex-wrap: wrap;
                
                >a {
                    font-size: 14px;
                    display: block;
                    padding: 5px 20px;
                    margin-bottom: 10px;

                    &:hover {
                        color: #39bf3e;
                    }
                }

                .active {
                    color: #fff;
                    background-color: #39bf3e;

                    &:hover {
                        color: #fff;
                    }
                }
            }
        }
    }

    .purchase-list {
        width: 100%;
        padding-left: 40px;
        padding-right: 55px;
        min-height: 400px;
        margin-top: 30px;
        border-bottom: 1px solid #f4f4f4;

        .list-header {
            display: flex;
            justify-content: space-between;
            padding-bottom: 10px;
            user-select: none;
            border-bottom: 1px solid #f4f4f4;

            >div {
                font-size: 14px;
                font-weight: bold;
            }
        }

        .list-item {
            display: flex;
            justify-content: space-between;
            font-size: 14px;
            margin: 15px 0;

            &:hover {
                background: #fbfbfb;

                .col-7>a {
                    color: #fff;
                    background-color: #39bf3e;
                }
            }
        }

        .col {
            height: 25px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .col-1 {
            width: 120px;
        }
        .col-2 {
            width: 100px;
        }
        .col-3 {
            width: 240px;
        }
        .col-4 {
            width: 180px;
        }
        .col-5 {
            width: 120px;
        }
        .col-6 {
            width: 120px;
        }
        .col-7 {
            text-align: center;
            width: 80px;

            >a {
                display: block;
                font-size: 12px;
                color: #39bf3e;
                border: 1px solid #39bf3e;
                padding: 3px 10px;
                transition: all .2s;
            }
        }
    }

    .pagination {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 60px;
    }
}
</style>
